package com.wzl.mixed.controller;

import com.wzl.common.VkPublicResponse;
import com.wzl.common.errcode.VkErrorCodeEnum;
import com.wzl.common.exception.VkPublicException;
import com.wzl.mixed.service.MpTryCatchSevice;
import com.wzl.mixed.service.impl.MpTryCatchServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * TryCatch异常捕获处理
 *
 * @author weizhongli
 * @since 1.0, 2022/11/15 10:35
 */
@Controller
@Slf4j
@RequestMapping("tryCatch")
public class MpTryCatchController {

//    @Autowired
//    private static MpTryCatchSevice mpTryCatchSevice;

    /**
     * 多层异常处理捕获
     */
    @RequestMapping("/test1")
    public static VkPublicResponse tryCatchTest1(){
        VkPublicResponse vkPublicResponse = new VkPublicResponse();
        try{
            MpTryCatchSevice mpTryCatchSevice = new MpTryCatchServiceImpl();
            mpTryCatchSevice.getMssage1();
        } catch (VkPublicException e) {
            log.error("外层VkPublicException报错啦");
            vkPublicResponse.setResultInfo(e);
        } catch (Exception e) {
            log.error("外层Exception报错啦");
            vkPublicResponse.setResultInfo(VkErrorCodeEnum.ERROR);
        }
        return vkPublicResponse;
    }

    @RequestMapping("/test2")
    public static VkPublicResponse tryCatchTest2(){
        VkPublicResponse vkPublicResponse = new VkPublicResponse();
        try{
            MpTryCatchSevice mpTryCatchSevice = new MpTryCatchServiceImpl();
            mpTryCatchSevice.getMssage2();
        } catch (VkPublicException e) {
            log.error("外层VkPublicException报错啦");
            vkPublicResponse.setResultCode(e.getErrCode());
            vkPublicResponse.setResultMsg(e.getMessage());
        } catch (Exception e) {
            log.error("外层Exception报错啦");
            vkPublicResponse.setResultCode(VkErrorCodeEnum.ERROR.getCode());
            vkPublicResponse.setResultMsg(VkErrorCodeEnum.ERROR.getMessage());
        }
        return vkPublicResponse;
    }

    /**
     * 实例1
     * 结果：外层输出-> 内层异常-> 后续处理业务
     */
    public static void out1(){
        try {
            log.info("外层输出");
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
            }
        }catch (Exception e){
            log.error("外层异常",e);
        }
    }

    /**
     * 实例2 内层try catch中添加throw new RuntimeException("内层抛出异常",e)将异常抛出
     * 结果：外层输出-> 内层异常-> 外层异常-> 后续处理业务
     */
    public static void out2(){
        try {
            log.info("外层输出");
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                //向外抛出异常
                throw new RuntimeException("这是内层抛出的异常",e);
            }
        }catch (Exception e){
            log.error("外层异常",e);
        }
    }

    /**
     * 实例3 在外层try catch中再次将异常抛出throw new RuntimeException("外层抛出异常",e);
     * 结果：外层输出-> 内层异常-> 外层异常
     */
    public static void out3(){
        try {
            log.info("外层输出");
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }
        }catch (Exception e){
            log.error("外层异常",e);
            //向外抛出异常
            throw new RuntimeException("外层抛出异常",e);
        }
    }

    /**
     * 实例4 out()方法中的异常代码不在try 代码块中，且主方法也没有对out()方法进行异常捕获处理
     * 结果 直接报错  处理方法：out()的上层方法即主方法中,对out()进行异常处理
     * 下层方法中的异常代码没有在try 代码块中，只需在上层方法中进行异常处理即可
     */
    public static void out4(){
        //添加异常代码，不在try 代码块中
        int j = 3/0;
        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
            throw new RuntimeException("外层抛出异常",e);
        }
    }


    public static void main(String[] args) {
        //VkPublicResponse vkPublicResponse = tryCatchTest1();
        //VkPublicResponse vkPublicResponse = tryCatchTest2();
        //log.info(String.valueOf(vkPublicResponse));

        out4();
        log.info("后续处理业务");

    }

}